/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package framework.agendaFactory;

import java.awt.event.ActionEvent;

/**
 *
 * @author Sergio Carlos Peres
 */
public class ButtonListener implements java.awt.event.ActionListener{

    protected boolean altere = false;
    protected framework.agendaFactory.lib.InternalFrame internalFrame;
    
    public ButtonListener(framework.agendaFactory.lib.InternalFrame internalFrame) {
        this.internalFrame = internalFrame;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        String nomeVariavel = trataGetSource(e.getSource().toString());
        
        switch (nomeVariavel) {
            case "txtPesquisar" : txtPesquisarActionPerformed(e); break;
            case "btnPesquisar" : btnPesquisarActionPerformed(e); break;
            case "cboPesquisar" : cboPesquisarActionPerformed(e); break;
            case "btnNovo"      : btnNovoActionPerformed(e);      break;
            case "btnAlterar"   : btnAlterarActionPerformed(e);   break;
            case "btnSalvar"    : btnSalvarActionPerformed(e);    break;
            case "btnCancelar"  : btnCancelarActionPerformed(e);  break;
            case "btnExcluir"   : btnExcluirActionPerformed(e);   break;
            
            default:
                adicionalActionPerformaed(e, nomeVariavel);
                //System.out.println("pau!!! " + nomeVariavel + "\n" + e.getSource().toString());
                break;
        }
    }
    
    protected String trataGetSource(String getSource) {
        int i, j;
        for (i = 0; getSource.charAt(i) != '['; i++);
        for (j = ++i; getSource.charAt(j) != ','; j++);
        return getSource.substring(i, j);
    }
    
    protected void interfacePadrao(String sql, String visibilidade, boolean ativarCamposAdicionar, int abaVisivel) {
        internalFrame.preencherTabela(sql);
        interfacePadrao(visibilidade, ativarCamposAdicionar, abaVisivel);
    }
    
    protected void interfacePadrao(String visibilidade, boolean ativarCamposAdicionar, int abaVisivel) {
        internalFrame.limparAdicionar();
        internalFrame.getToolBar().visibilidade(visibilidade);
        internalFrame.ativarCamposAdicionar(ativarCamposAdicionar);
        internalFrame.getTabbedPane().setSelectedIndex(abaVisivel);
    }
    
    protected void pesquisar(String tabela, String campo) {
        String sql = "SELECT * FROM " + tabela + " WHERE " + campo + " LIKE '" 
                + internalFrame.getToolBar().getTxtPesquisar().getText() + "%'";
        interfacePadrao(sql, "", false, 0);
        internalFrame.getToolBar().getTxtPesquisar().requestFocus();
        internalFrame.getToolBar().getTxtPesquisar().selectAll();
    }
    
    protected void pesquisaCombobox(String tabela, String campo) {
        String sql = "SELECT * FROM " + tabela + " ";
        if (!internalFrame.getToolBar().getCboPesquisar().getSelectedItem().toString().equals(":: Todos ::")) {
            sql = sql + " WHERE " + campo + " LIKE '" 
                    + internalFrame.getToolBar().getCboPesquisar().getSelectedItem().toString() + "%'";
        }
        interfacePadrao(sql, "", false, 0);
    }
    
    // Metodos para presonalizar.
    public void txtPesquisarActionPerformed(ActionEvent e) {}
    public void btnPesquisarActionPerformed(ActionEvent e) {}
    public void cboPesquisarActionPerformed(ActionEvent e) {}
    public void btnNovoActionPerformed(ActionEvent e) {
        interfacePadrao("novo", true, 1);
    }
    public void btnAlterarActionPerformed(ActionEvent e) {}
    public void btnSalvarActionPerformed(ActionEvent e) {}
    public void btnCancelarActionPerformed(ActionEvent e) {
        this.altere = false;
        interfacePadrao("", false, 0);
    }
    public void btnExcluirActionPerformed(ActionEvent e) {}
    // Metodo para adicionar mais ActionPerfomead.
    public void adicionalActionPerformaed(ActionEvent e, String nomeVariavel) {}
}
